//
//  PublicFun.h
//  tcpoctest
//
//  Created by gump on 10/25/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#ifndef tcpoctest_PublicFun_h
#define tcpoctest_PublicFun_h

#import <CommonCrypto/CommonDigest.h>

CG_INLINE UIImage* OriginImage(UIImage * image ,CGSize size)
{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
    
    // 绘制改变大小的图片
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    
    // 返回新的改变大小后的图片
    return scaledImage;
}

CG_INLINE UIButton* creatImageButton(CGRect rc,
                                     UIImage *normalimage,
                                     UIImage *pressimage,
                                     id tag,
                                     SEL action)
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = rc;
    btn.backgroundColor=[UIColor clearColor];
    normalimage = OriginImage(normalimage, rc.size);
    pressimage = OriginImage(pressimage, rc.size);
    [btn setBackgroundImage:normalimage forState:UIControlStateNormal];
    [btn setBackgroundImage:pressimage forState:UIControlStateSelected];
    [btn addTarget:tag action:action forControlEvents:UIControlEventTouchUpInside];
    
    return btn;
}

CG_INLINE NSString* getProvinceFromMapArr(int province)
{
    if (province > -1 && province < [[DataControl shareControl].mapArray count])
    {
        return [[[DataControl shareControl].mapArray objectAtIndex:province] objectForKey:@"State"];
    }
    else
    {
        return @"dezhou";
    }
    
}

CG_INLINE NSString* getCityFromMapArr(int province,int city)
{
    if (province < 0 || province >= [[DataControl shareControl].mapArray count])
    {
        return @"dezhou";
    }
    
    if (city < 0 || city >=
        [[[[DataControl shareControl].mapArray objectAtIndex:province]
        objectForKey:@"Cities"] count])
    {
        return @"dezhou";
    }
    
    return [[[[[DataControl shareControl].mapArray objectAtIndex:province]
              objectForKey:@"Cities"]
             objectAtIndex:city]
            objectForKey:@"city"];
}

CG_INLINE NSString* md5(NSString *str) {
    
    
    
    const char *cStr = [str UTF8String];
    
    
    
    unsigned char result[16];
    
    
    
    CC_MD5( cStr, strlen(cStr), result );
    
    
    
    return [NSString stringWithFormat:

            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            
            result[0],result[1],result[2],result[3],
            
            result[4],result[5],result[6],result[7],
            
            result[8],result[9],result[10],result[11],
            
            result[12],result[13],result[14],result[15]];
    
}

CG_INLINE NSString* getFileNameWithDate(int index)
{
    //[NSDate date]获取的是GMT时间，不是当前时区的时间
    NSDate *date = [NSDate date];
    //从系统当前时区新建一个时区
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    //从GMT时间得到当前时区的总的秒数
    NSInteger interval = [zone secondsFromGMTForDate:date];
    //从上面得到的秒数获得当前时区的时间
    NSDate *localDate = [date dateByAddingTimeInterval:interval];
    //以特定格式得到当前时间的字符串
    NSDateFormatter *form = [[NSDateFormatter alloc] init];
    //y年M月d日 H时m分s秒（注意M和m）
    [form setDateFormat:@"yyyyMMddHHmmss"];
    NSString *str = [form stringFromDate:localDate];
    NSString *strext = @"";
    switch (index)
    {
        case FT_HEADIMAGE:
        {
            strext = @"himage";
        }
            break;
        case FT_AUDIO:
        {
            strext = @"audio";
        }
            break;
        case FT_CHAT:
        {
            strext = @"chat";
        }
            break;
        case FT_FIRCIR:
        {
            strext = @"fircir";
        }
            break;
        case FT_FCBGI:
        {
            strext = @"fcbgi";
        }
            break;
        case FT_GIFT:
        {
            strext = @"gift";
        }
            break;
        default:
            break;
    }
    
    NSString *strname = [NSString stringWithFormat:@"%@%@",str,strext];
    
    return strname;
}

CG_INLINE NSString* getLocalMail()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                         @"" forKey:@"currentUser"]];
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"currentUser"];
}

CG_INLINE int getLocalPP()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                      [NSNumber numberWithInt:-1] forKey:@"currentPP"]];
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"currentPP"];
}

CG_INLINE void setLocalMail(NSString *mail)
{
    [[NSUserDefaults standardUserDefaults] setObject:mail
                                              forKey:@"currentUser"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

CG_INLINE void setLocalPP(int ppnum)
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:ppnum]
                                                forKey:@"currentPP"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

CG_INLINE bool IsFirstLogin()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                         [NSNumber numberWithBool:YES]forKey:@"firstLaunch"]];
    
    bool isfirst = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"];
    
    if (isfirst) {
        [[NSUserDefaults standardUserDefaults] setBool:false forKey:@"firstLaunch"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    return isfirst;
}

CG_INLINE int getNearPeopleFilterType()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                         [NSNumber numberWithInt:NTF_ALL] forKey:@"nearpeoplefiltertype"]];
    
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"nearpeoplefiltertype"];
}

CG_INLINE void setNearPeopleFilterType(int ftype)
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:ftype]
                                              forKey:@"nearpeoplefiltertype"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

CG_INLINE int getRankListDataFilterType()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                         [NSNumber numberWithInt:RLI_RICH_MONTH] forKey:@"ranklistdatafiltertype"]];
    
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"ranklistdatafiltertype"];
}

CG_INLINE void setRankListDataFilterType(int ftype)
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:ftype]
                                              forKey:@"ranklistdatafiltertype"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

CG_INLINE int getRankListKindFilterType()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                         [NSNumber numberWithInt:RLI_RICH_DAY] forKey:@"ranklistkindfiltertype"]];
    
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"ranklistkindfiltertype"];
}

CG_INLINE void setRankListKindFilterType(int ftype)
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:ftype]
                                              forKey:@"ranklistkindfiltertype"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

CG_INLINE int getLocalSequence()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                         [NSNumber numberWithInt:0] forKey:@"localsequence"]];
    
    int s = [[NSUserDefaults standardUserDefaults] integerForKey:@"localsequence"];
    
    if (s + 1 > SEQUENCE_MAX) {
        s = 0;
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:s + 1]
                                              forKey:@"localsequence"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return s;
}

CG_INLINE void setLocalSequence(int s)
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:s]
                                              forKey:@"localsequence"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

CG_INLINE NSString* stringWithUUID()
{
    CFUUIDRef    uuidObj = CFUUIDCreate(nil);
    NSString    *uuidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(nil, uuidObj));
    CFRelease(uuidObj);
    return uuidString;
}

CG_INLINE NSString* getUUID()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                         @"" forKey:@"UUID"]];
    NSString    *uuidString = [[NSUserDefaults standardUserDefaults] stringForKey:@"UUID"];
    if ([uuidString isEqualToString:@""])
    {
        uuidString = stringWithUUID();
        
        [[NSUserDefaults standardUserDefaults] setObject:uuidString
                                                  forKey:@"UUID"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    return uuidString;
}

CG_INLINE NSString* getDateString()
{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString *curdatestr = [dateFormat stringFromDate:[NSDate date]];
    
    dateFormat = nil;
    
    return curdatestr;
}

CG_INLINE NSString* getDateStringWithSlant()
{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    
    [dateFormat setDateFormat:@"yyyy/MM/dd/HH/mm/ss"];
    
    NSString *curdatestr = [dateFormat stringFromDate:[NSDate date]];
    
    dateFormat = nil;
    
    return curdatestr;
}

CG_INLINE bool IsHaveFirendCircle()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                         [NSNumber numberWithBool:NO]forKey:@"firendcircle"]];
    
    bool isfirst = [[NSUserDefaults standardUserDefaults] boolForKey:@"firendcircle"];
    
    if (isfirst == NO) {
        [[NSUserDefaults standardUserDefaults] setBool:true forKey:@"firendcircle"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    return isfirst;
}

CG_INLINE int getOnLineOrder()
{
    [[NSUserDefaults standardUserDefaults]registerDefaults:[NSDictionary dictionaryWithObject:                                                         [NSNumber numberWithInt:OLOT_NULL]forKey:@"onlineorder"]];
    
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"onlineorder"];
}

CG_INLINE void setOnLineOrder(int io)
{
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:io]
                                              forKey:@"onlineorder"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#endif
